package com.gjy.sentinel.core.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-10-14 17:15:37
 */
public class JsonUtil {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    /** 读取单个对象（不常用） */
    public static <T> T readJson(File file, Class<T> clz) throws IOException {
        return MAPPER.readValue(file, clz);
    }

    /** 读取 List<T> */
    public static <T> List<T> readJsonList(File file, Class<T> elementClz) throws IOException {
        return MAPPER.readValue(file, new TypeReference<List<T>>() {
        });
    }

    /** 写入 JSON（美化） */
    public static void writeJson(File file, Object obj) throws IOException {
        MAPPER.writerWithDefaultPrettyPrinter().writeValue(file, obj);
    }

}
